<template>
  <!-- 下拉菜单 -->
  <span class="drop">
    <el-dropdown
      v-bind="dropdownAttrs"
      :placement="dropdownAttrs.placement ? dropdownAttrs.placement : 'bottom'"
      @command="handleCommand"
    >
      <el-button
        class="lidus"
        :loading="isLoading"
        :disabled="disabled"
      >
        <slot>
          <span class="value">{{ activeItem.label || '请选择' }}</span>
          <i class="el-icon-caret-bottom dropdown-icon" />
        </slot>
      </el-button>
      <el-dropdown-menu
        slot="dropdown"
        class="list"
        :class="popoverClass"
      >
        <el-dropdown-item
          v-for="(item, index) in dropList"
          :key="index"
          class="drop-item"
          :command="item"
          :class="{ current: item.label === activeItem.label }"
        >
          <span
            :style="itemStyle"
            class="text"
          >{{ item.label }}</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </span>
</template>

<script>
export default {
  name: 'DropdownMenu',
  props: {
    dropList: {
      type: Array,
      default: () => [],
      require: true
    },
    // 默认选择项 需要用.sync修饰符
    currentItem: {
      type: Object,
      default: () => ({}),
      require: true
    },
    // 可下拉popover的class调整样式
    popoverClass: {
      type: String,
      default: ''
    },
    // el-dropdown的Attrs
    dropdownAttrs: {
      type: Object,
      default: () => ({})
    },
    disabled: {
      type: Boolean,
      default: false
    },
    itemStyle: {
      type: Object,
      default: () => ({})
    },
    isLoading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
    }
  },
  computed: {
    activeItem: {
      get() {
        return this.currentItem
      },
      set(val) {
        this.$emit('update:current-item', val)
      }
    }
  },
  methods: {
    // 改变
    handleCommand(command) {
      this.activeItem = command
      this.$emit('dropChange', command)
    }
  }
}
</script>

<style lang="scss" scoped>
.drop {
  display: inline-block;
  font-size: 14px;
  font-weight: 400;
  cursor: pointer;
  position: relative;
  .el-dropdown {
    color: inherit;
    font-size: inherit;
    .active {
      color: inherit;
      i {
        color: inherit;
      }
    }
    .lidus {
      border: none;
      background: none;
      color: inherit;
      cursor: pointer;
      padding: 0;
      &:disabled {
        color: inherit;
        cursor: no-drop;
        opacity: 80%;
      }
      i {
        transition: all 0.4s;
      }
      &:focus {
        outline: none;
      }
    }

    // .now-hover {
    //   color: #2f54eb;
    //   .value {
    //     color: inherit;
    //   }
    //   i {
    //     transform: rotate(180deg);
    //     transform-origin: center;
    //     color: inherit;
    //   }
    // }
  }
}

// ------------------------------------------

.el-dropdown-menu {
  min-width: 80px;
  max-height: 400px;
  overflow-y: auto;
  text-align: center;
  ::v-deep .popper__arrow {
    visibility: hidden;
  }
  ::v-deep .el-dropdown-menu__item {
    color: #333333;
    line-height: 30px;
    font-size: 14px;
  }

  .drop-item {
    &:hover {
      background-color: #f0f5ff;
      color: #0062ff;
    }
    .text {
      display: inline-block;
    }
  }
  .current {
    color: #0062ff !important;
  }
}
</style>
